/**
 * 
 */
package junior.util.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.util.ResourceUtils;

/**
 * @author bo.wu
 * @CreateDate Oct 28, 2015
 */
public class PropertiesConfigContext extends AbstractConfigContext {
	private Properties properties;
	
	public PropertiesConfigContext() {}
	
	public PropertiesConfigContext(String propertiesFilePath) {
		try{
			File file = ResourceUtils.getFile(propertiesFilePath);
			properties = new Properties();
			InputStream is = new FileInputStream(file);
			properties.load(is);
			is.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public PropertiesConfigContext(Properties properties) {
		setProperties(properties);
	}
	
	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	@Override
	protected Map<String, String> loadConfigs() {
		Enumeration<?> keys = properties.propertyNames();
		
		Map<String, String> map = new HashMap<String, String>(properties.size());
		
		while (keys.hasMoreElements()) {
			String key = (String) keys.nextElement();
			map.put(key, properties.getProperty(key));
		}
		
		return map;
	}

}
